<?php
$cargo = $data[0];
$banca = $data[1];
$orgao = $data[2];
$tipoQuestao = $data[3];
$assuntoDisciplina = $data[4];

if (isset($data[5][0])) {
    $questao = $data[5][0];
    $action = "update";
} else {
    $questao = "";
    $action = "insert";
}

if (isset($data[6])) {
    $respostas = $data[6];
} else {
    $respostas = "";
}
?>

<div class="section">
    <div class="box">
        <div class="title">
            Formulário de questões
            <span class="hide"></span>
        </div>
        <div class="content">
            <form action="<?php echo BASE_URL . "questoes/" . $action ?>" id="form" method="post" enctype="multipart/form-data" onsubmit="return __validaForm();">
                <?php
                if (isset($questao->codigo) && !empty($questao->codigo)) {
                    echo "<input type='hidden' name='codigo' value='" . $questao->codigo . "' id='codigo-questao'/>";
                }
                ?>
                <!--                <div class="row">
                                    <label>Texto</label>
                -->
                <div class="right"><input type="hidden" value="" name="texto" class=""
                    <?php
                    if (isset($questao->texto)) {
                        echo " value=' $questao->texto'";
                    }
                    ?>/></div>
                <!--</div>-->
                <div class="row">
                    <label>Descrição</label>
                    <div class="right"><textarea rows="" cols="" name="descricao" style="height : 100px;" class="required"><?php
                            if (isset($questao->descricao)) {
                                echo $questao->descricao;
                            }
                            ?></textarea></div>
                </div>
                <div class="row">
                    <label>Imagem</label>
                    <div class="right"><input type="file" name="imagem" />
                        <?php
                        if (isset($questao->imagem)) {
                            echo "<a href='" . BASE_URL . "uploads/" . $questao->imagem . "' target='_blank'><img src=" . BASE_URL . "uploads/" . $questao->imagem . " width='35px' height='35px' class='f-right' /></a>";
                        }
                        ?>
                    </div>

                </div>
                <div class="row">
                    <label>Banca</label>
                    <div class="right">
                        <select class="required" name="banca" id="banca">
                            <option selected="selected" value="">Escolha a Banca</option>
                            <?php
                            foreach ($banca as $row) {
                                echo "<option value='$row->codigo'";
                                if (isset($questao->Banca) && $questao->Banca->codigo == $row->codigo) {
                                    echo " SELECTED";
                                }
                                echo ">$row->nome</option>";
                            }
                            ?>

                        </select>
                    </div>
                </div>
                <div class="row">
                    <label>Cargo</label>
                    <div class="right">
                        <select class="required" name="cargo">
                            <option selected="selected" value="">Escolha o Cargo</option>
                            <?php
                            foreach ($cargo as $row) {
                                echo "<option value='$row->codigo'";
                                if (isset($questao->Cargo) && $questao->Cargo->codigo == $row->codigo) {
                                    echo " SELECTED";
                                }
                                echo ">$row->nome</option>";
                            }
                            ?>
                        </select>
                    </div>
                </div>
                <div class="row">
                    <label>Orgao</label>
                    <div class="right">
                        <select class="required" name="orgao">
                            <option selected="selected" value="">Escolha o Orgao</option>
                            <?php
                            foreach ($orgao as $row) {
                                echo "<option value='$row->codigo'";
                                if (isset($questao->Orgao) && $questao->Orgao->codigo == $row->codigo) {
                                    echo " SELECTED";
                                }
                                echo ">$row->nome</option>";
                            }
                            ?>

                        </select>
                    </div>
                </div>
                <div class="row">
                    <label>Disciplina/Assunto</label>
                    <div class="right">
                        <select class="required" name="assuntoDisciplina">
                            <option selected="selected" value="">Escolha o Assunto</option>
                            <?php
                            foreach ($assuntoDisciplina as $row) {
                                echo "<option value='$row->codigo'";
                                if (isset($questao->AssuntoDisciplina) && $questao->AssuntoDisciplina->codigo == $row->codigo) {
                                    echo " SELECTED";
                                }echo ">" . $row->Disciplina->descricao . "/" . $row->Assunto->descricao . "</option>";
                            }
                            ?>

                        </select>
                    </div>
                </div>
                <div class="row">
                    <label>Ano</label>
                    <div class="right grid_3">
                        <input type="text" name="ano" id="ano" class="input-140 required" 
                        <?php
                        if (isset($questao->ano)) {
                            echo " value=' $questao->ano'";
                        }
                        ?>/></div>
                </div>
                <div class="row">
                    <label>Tipo Questão</label>
                    <div class="right">
                        <select class="required" name="tipoquestao">
                            <option selected="selected" value="">Escolha um tipo</option>
                            <?php
                            foreach ($tipoQuestao as $row) {
                                echo "<option value='$row->codigo'";
                                if (isset($questao->TipoQuestao) && $questao->TipoQuestao->codigo == $row->codigo) {
                                    echo " SELECTED";
                                }echo ">" . $row->descricao . "</option>";
                            }
                            ?>

                        </select>
                    </div>
                </div>

                <div class="row">
                    <button type="submit" class="blue right" id="save-questao"><span>Salvar</span></button>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="section">
    <div class="box">
        <div class="title">
            Respostas da questão
            <span class="hide"></span>
        </div>
        <div class="content">
            <?php
            if (isset($questao->codigo) && !empty($questao->codigo)) {
                echo "<button type='button' class='green new-respostas'><span>Nova Resposta</span></button>";
                echo "<div id='respostas-questao'></div>";
            } else {
                echo "Insira a questão antes de inserir as respostas!";
            }
            ?>
        </div>
    </div>
</div>